package com.cheer.dao;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.cheer.entity.ExPresentAudioEntity;
import com.cheer.entity.ExPresentAudioGoodEntity;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 用户信息
 *
 * @author cheer
 */
@Mapper
@Repository
public interface ExPresentAudioGoodDao extends BaseMapper<ExPresentAudioGoodEntity> {

    default List<ExPresentAudioGoodEntity> selectByPage(Integer pageNum, Integer pageSize) {
        return selectList(new QueryWrapper<ExPresentAudioGoodEntity>()
                .orderByDesc("vote")
                .last("limit " + (pageNum - 1) * pageSize + ", " + pageSize));
    }

    default List<ExPresentAudioGoodEntity> selectByPageOrder(Integer pageNum, Integer pageSize) {
        return selectList(new QueryWrapper<ExPresentAudioGoodEntity>()
                .orderByDesc("create_time")
                .last("limit " + (pageNum - 1) * pageSize + ", " + pageSize));
    }

    default List<ExPresentAudioGoodEntity> selectByType(Integer pageNum, Integer pageSize, Integer type) {
        return selectList(new QueryWrapper<ExPresentAudioGoodEntity>()
                .eq("type", type)
                .orderByDesc("vote")
                .last("limit " + (pageNum - 1) * pageSize + ", " + pageSize));
    }

    default ExPresentAudioGoodEntity selectByLimit(Integer number) {
        return selectOne(new QueryWrapper<ExPresentAudioGoodEntity>()
                .orderByDesc("vote")
                .last("limit " + number + ", 1"));
    }

    default ExPresentAudioGoodEntity selectByRandom(Integer random) {
        return selectOne(new QueryWrapper<ExPresentAudioGoodEntity>()
                .last("limit " + random + ", 1"));
    }

    Integer countByPath(String path);

    Integer countByPathTotal(String path);

    default int deleteByUserId(Integer id) {
        return delete(new QueryWrapper<ExPresentAudioGoodEntity>().eq("user_id", id));
    }

    default ExPresentAudioGoodEntity selectByVote(Integer vote) {
        return selectOne(new QueryWrapper<ExPresentAudioGoodEntity>()
                .lt("vote", vote)
                .orderByDesc("vote")
                .last("limit 1"));
    }

    void incrVote(Integer id);

    void decrVote(Integer id);

    default void updateByUserId(ExPresentAudioGoodEntity entity) {
        update(entity, new QueryWrapper<ExPresentAudioGoodEntity>().eq("user_id", entity.getUserId()));
    }

    default List<ExPresentAudioGoodEntity> selectByPageInUserIds(Integer pageNum, Integer pageSize, List<Integer> userIds) {
        return selectList(new QueryWrapper<ExPresentAudioGoodEntity>()
                .in("user_id", userIds)
                .last("limit " + (pageNum - 1) * pageSize + ", " + pageSize));
    }
}
